-- dragon_breath
-- create by huanghr
-- 技能: 周围格子3乘3翻开。以目标为中心，对其同一列范围敌人造成伤害=魔力 * 威力 * 加成


return {
    apply = function(source, target, skillId, round, para, extra)
        local magic = source:getMagic();
        local base = magic;

        local coef = para[1];
        local aroundCoef = para[2];
        local range = para[3];

        --最终威力
        local force = math.modf(base * coef / 1000);

        -- 3. 伤害/计算伤害时魔力+3
        local aroundDamage = math.modf(force * aroundCoef / 1000);

        -- 4. 遍历怪物所在列
        local ret = {};
        local pos = target.dbase:query("pos");
        if not pos then
            pos = target:getOwner();
        end

        local cover = DungeonM.getAroundGrids(pos);
        --
        local grid = DungeonM.getGridByPos(pos);

        -- 如果是未翻开的格子
        if  not grid:isOpened()  then
            return;
        end

        -- 如果是未死亡的怪物
        if  grid:isMonster() or
            not grid.monster:isDead() then
                -- 增加命中动作
                if SkillM.getSequence(grid.monster) == nil then
                    SkillM.getSequenceList()[grid.monster] = CastSequence.create(source, grid.monster, skillId);
                end

                SkillM.getSequence(grid.monster):hit(source, grid.monster, skillId);

                -- 受创
                CombatM.receiveDamage(source, grid.monster, force, skillId, extra);

                -- 目标
                table.insert(ret, grid.monster);
        else
            return ret;
        end

        for _, m in pairs(cover) do
            grid = DungeonM.getGridByPos(m);

            -- 如果是未翻开的格子
            if  not grid:isOpened()  then
                grid:open();
            end

            -- 如果是未死亡的怪物
            if  grid:isMonster() and
                not grid.monster:isDead() then
                -- 增加命中动作
                if SkillM.getSequence(grid.monster) == nil then
                    SkillM.getSequenceList()[grid.monster] = CastSequence.create(source, grid.monster, skillId);
                end

                SkillM.getSequence(grid.monster):hit(source, grid.monster, skillId);

                -- 受创
                CombatM.receiveDamage(source, grid.monster, aroundDamage, skillId, extra);

                -- 目标
                table.insert(ret, grid.monster);
            end
        end

        -- 返回作用目标
        return ret;
    end,

    parse = function(desc)
        local arr = string.explode(desc, ",");
        return { tonumber(arr[1]), tonumber(arr[2]), tonumber(arr[3]), };
    end,

    -- 解析技能描述
    parseDesc = function(source, skillId, desc, para)
        local magic = source:getMagic();
        local base = magic;

        local coef = para[1];
        local aroundCoef = para[2] / 10;

        --最终威力
        local force = math.modf(base * coef / 1000);
        desc = string.gsub(desc, "{damage}", force);
        desc = string.gsub(desc, "{around_coef}", aroundCoef);
        return desc;
    end,
}
